/**
 * 
 */
package com.justspeak.module;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.justspeak.R;

/**
 * @author Administrator
 * 
 */
public class CourseInfoAdapter extends BaseExpandableListAdapter {

    public class ExpandableListHolder { // 定义一个内部类，用于保存listitem的3个子视图引用,2个textview和1个checkbox
	public TextView tvName;
	public CheckBox chkChecked;
    }

    private Context context; // 父activity
    private LayoutInflater mChildInflater; // 用于加载listitem的布局xml
    private LayoutInflater mGroupInflater; // 用于加载group的布局xml
    private List<UnitInfo> groups; // 所有group

    // 构造方法：参数c － activity，参数group － 所有group

    public CourseInfoAdapter(Context c, List<UnitInfo> groups) {
	context = c;
	mChildInflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	mGroupInflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	this.groups = groups;
    }

    /**
     * 根据组索引和子元素索引，取得子元素对象
     */
    public Object getChild(int groupPosition, int childPosition) {
	UnitInfo unit = groups.get(groupPosition);
	CourseInfo course = unit.getChildren().get(childPosition);
	return course;
    }

    /**
     * 返回子元素索引
     */
    public long getChildId(int groupPosition, int childPosition) {

	return childPosition;
    }

    /**
     * 根据组索引返回分组的子item数
     */
    public int getChildrenCount(int groupPosition) {

	return this.groups.get(groupPosition).getChildren().size();
    }

    /**
     * 根据组索引返回组对象
     */
    public Object getGroup(int groupPosition) {
	return this.groups.get(groupPosition);
    }

    /**
     * 返回分组数
     */
    public int getGroupCount() {
	return this.groups.size();
    }

    /**
     * 返回分组索引
     */
    public long getGroupId(int groupPosition) {
	return groupPosition;
    }

    /**
     * 行是否具有唯一id
     */
    public boolean hasStableIds() {
	return false;
    }

    /**
     * 行是否可选
     */
    public boolean isChildSelectable(int groupPosition, int childPosition) {
	return true;
    }

    /**
     * 根据组索引渲染"组视图"
     */
    public View getGroupView(int groupPosition, boolean isExpanded, View view,
	    ViewGroup parent) {
	ExpandableListHolder holder = null;
	if (view == null) { // 判断view（即view是否已构建好）是否为空

	    // 若组视图为空，构建组视图。注意flate的使用，R.layout.browser_expandable_list_item代表了
	    // 已加载到内存的browser_expandable_list_item.xml文件

	    view = mGroupInflater.inflate(com.justspeak.R.layout.course_item,
		    null);
	    // 下面主要是取得组的各子视图，设置子视图的属性。用tag来保存各子视图的引用
	    holder = new ExpandableListHolder();
	    // 从view中取得textView
	    holder.tvName = (TextView) view.findViewById(R.id.tv_course_name);
	    // 从view中取得checkbox
	    holder.chkChecked = (CheckBox) view
		    .findViewById(R.id.chb_course_name);
	    // holder.chkChecked.setEnabled(false);//禁用checkbox
	    // 把checkbox、textview的引用保存到组视图的tag属性中
	    view.setTag(holder);
	} else { // 若view不为空，直接从view的tag属性中获得各子视图的引用
	    holder = (ExpandableListHolder) view.getTag();
	}
	// 对应于组索引的组数据（模型）
	UnitInfo info = this.groups.get(groupPosition);
	if (info != null) {
	    // 根据模型值设置textview的文本
	    holder.tvName.setText(info.getTitle());
	    // 根据模型值设置checkbox的checked属性
	    holder.chkChecked.setChecked(info.getChecked());
	    holder.chkChecked.setTag(info);
	    holder.chkChecked.setOnClickListener(new OnClickListener() {

		public void onClick(View v) {
		    UnitInfo group = (UnitInfo) v.getTag();
		    group.setChecked(!group.getChecked());
		    notifyDataSetChanged();

		}

	    });
	}
	// TODO Auto-generated method stub
	return view;

    }

    /**
     * 行渲染方法
     */
    public View getChildView(int groupPosition, int childPosition,
	    boolean isLastChild, View convertView, ViewGroup parent) {
	// TODO Auto-generated method stub
	ExpandableListHolder holder = null; // 清空临时变量
	if (convertView == null) { // 若行未初始化
	    // 通过flater初始化行视图
	    convertView = mChildInflater.inflate(R.layout.course_item, null);
	    // 并将行视图的3个子视图引用放到tag中
	    holder = new ExpandableListHolder();
	    holder.tvName = (TextView) convertView
		    .findViewById(R.id.tv_course_name);

	    holder.chkChecked = (CheckBox) convertView
		    .findViewById(R.id.chb_course_name);
	    // holder.chkChecked.setEnabled(false);
	    convertView.setTag(holder);
	} else { // 若行已初始化，直接从tag属性获得子视图的引用
	    holder = (ExpandableListHolder) convertView.getTag();
	}
	// 获得行数据（模型）
	CourseInfo info = this.groups.get(groupPosition).getChildren().get(
		childPosition);

	if (info != null) {
	    // 根据模型数据，设置行视图的控件值
	    holder.tvName.setText(info.getMp3Name());
	    holder.chkChecked.setChecked(info.isChecked());
	    holder.chkChecked.setTag(info);
	    holder.chkChecked.setOnClickListener(new OnClickListener() {

		public void onClick(View v) {
		    CheckBox check = (CheckBox) v;
		    CourseInfo item = (CourseInfo) v.getTag();
		    item.setChecked(!item.isChecked());

		}

	    });
	}
	return convertView;
    }

}
